package com.example.s;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.adapter.MyFragmentPagerAdapter;
import com.example.s.application.MyApplication;
import com.example.s.fragment.ChatFragment;
import com.example.s.fragment.MainFragment;
import com.example.s.fragment.MyFragment;
import com.example.s.fragment.TeamFragment;
import com.example.s.fragment.UpLoadFragment;
import com.example.s.utils.LiveDataBus;
import com.example.s.view.MyViewPager;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private TabLayout navBottom;
    private MyViewPager vpCotainer;
    protected List<Fragment> fragmentList = new ArrayList<>();
    ;
    private MainFragment mainFragment;
    private TeamFragment circleFragment;
    private UpLoadFragment upLoadFragment;
    private ChatFragment chatFragment;
    private MyFragment myFragment;
    private ImageView upload;
    public static int jump = 0;

    private Animation animation;


    private void initView() {

        navBottom = (TabLayout) findViewById(R.id.nav_bottom);
        vpCotainer = (MyViewPager) findViewById(R.id.vp_cotainer);
        upload = (ImageView) findViewById(R.id.upload);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.getInstance(A);
        initView();
        LiveDataBus.getInstance().with("jump", Integer.class).setValue(jump);
        LiveDataBus.getInstance().with("jump", Integer.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer == 0)
                    return;
                vpCotainer.setCurrentItem(4);
            }
        });

        mainFragment = new MainFragment();
        circleFragment = new TeamFragment();
        upLoadFragment = new UpLoadFragment();
        chatFragment = new ChatFragment();
        myFragment = new MyFragment();
        mainFragment.setToParent(new ToParent() {
            @Override
            public void passMsg() {
                vpCotainer.setCurrentItem(4);
            }
        });
        fragmentList.add(mainFragment);
        fragmentList.add(circleFragment);
        fragmentList.add(upLoadFragment);
        fragmentList.add(chatFragment);
        fragmentList.add(myFragment);
        vpCotainer.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        vpCotainer.setOffscreenPageLimit(4);
        navBottom.setupWithViewPager(vpCotainer);
        navBottom.getTabAt(0).setCustomView(getview(R.mipmap.home_page_select, "首页"));
        navBottom.getTabAt(1).setCustomView(getview(R.mipmap.home_page_circle, "组队"));
        navBottom.getTabAt(2).setCustomView(getview(R.mipmap.home_page_upload, ""));
        navBottom.getTabAt(3).setCustomView(getview(R.mipmap.home_page_message, "消息"));
        navBottom.getTabAt(4).setCustomView(getview(R.mipmap.home_page_my, "我的"));
        navBottom.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d("", "onTabSelected: " + tab.getPosition());
                View iv = tab.getCustomView().findViewById(R.id.iv);
                LiveDataBus.getInstance().with("style", Integer.class).setValue(0);
                switch (tab.getPosition()) {
                    case 0:
                        iv.setBackgroundResource(R.mipmap.home_page_select);
                        break;
                    case 1:
                        iv.setBackgroundResource(R.mipmap.home_page_circle_select);
                        break;
                    case 2:

                        break;
                    case 3:
                        iv.setBackgroundResource(R.mipmap.home_page_message_select);
                        break;
                    case 4:
                        iv.setBackgroundResource(R.mipmap.home_page_my_select);
                        LiveDataBus.getInstance().with("style", Integer.class).setValue(4);
                        break;
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View iv = tab.getCustomView().findViewById(R.id.iv);
                switch (tab.getPosition()) {
                    case 0:
                        iv.setBackgroundResource(R.mipmap.home_page);
                        break;
                    case 1:
                        iv.setBackgroundResource(R.mipmap.home_page_circle);
                        break;
                    case 2:

                        break;
                    case 3:
                        iv.setBackgroundResource(R.mipmap.home_page_message);
                        break;
                    case 4:
                        iv.setBackgroundResource(R.mipmap.home_page_my);
                        break;
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        LiveDataBus.getInstance().with("style", Integer.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                Log.d(TAG, "onChanged: integer" + integer);
                if (integer == 4) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.purple_bg));
                } else {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.deep_purple_bg));
                }
            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpCotainer.setCurrentItem(2);
            }
        });


        /**
         * 底部导航动画
         */

        animation = AnimationUtils.loadAnimation(this, R.anim.bottom_to_top);
        animation.setDuration(300);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                navBottom.setAlpha(1);
                upload.setImageAlpha(1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        jump = 0;
        navBottom.startAnimation(animation);
        upload.startAnimation(animation);
    }

    @Override
    protected void onStop() {
        super.onStop();
        navBottom.setAlpha(0);
        upload.setImageAlpha(0);
    }

    private View getview(int icon, String titleText) {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.main_nav_bottom, null);
        if (icon == R.mipmap.home_page_upload) {
            return view;
        }
        ImageView iv = view.findViewById(R.id.iv);
        iv.setBackgroundResource(icon);
        TextView title = view.findViewById(R.id.title);
        if (!"".contentEquals(titleText))
            title.setText(titleText);
        return view;
    }

    public interface ToParent {
        void passMsg();
    }
}
